package com.delong.spring.handler.app;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.delong.cache.AppCache;
import com.delong.model.app.App;
import com.delong.model.app.Tags;
import com.delong.service.AppService;
import com.delong.service.TagsService;
import com.delong.spring.handler.BaseController;

/**
 * 应用标签管理
 * 
 * @author liyang
 *
 */
@Controller
@RequestMapping("/tags")
public class TagController extends BaseController {

	private TagsService service = new TagsService();
	private AppService appService = new AppService();

	@RequestMapping("/list")
	public String getAppTagsLit(Map<String, Object> model, ServletRequest req) {
		// HttpServletRequest request = (HttpServletRequest) req;
		model.put("username", this.getUser().getUsername());
		return "app/Tags";
	}

	@RequestMapping("/getall")
	@ResponseBody
	public String getAllElement(Map<String, Object> model, ServletRequest req) {
		TagsService tagsService = new TagsService();
		List<Tags> Tags = tagsService.getAllElement();
		JSONObject result = new JSONObject();
		result.put("rows", Tags);
		result.put("code", 0);
		return result.toJSONString();
	}

	@RequestMapping("/getdata")
	@ResponseBody
	public String getData(Map<String, Object> model, @RequestParam(value = "tags", defaultValue = "") String tags,
			@RequestParam(value = "page", defaultValue = "1") int page,
			@RequestParam(value = "rows", defaultValue = "15") int rows) {

		if (!tags.trim().equals("")) {
			tags = "%" + tags + "%";
		}
		List<Tags> elements = this.service.getElementByName(tags, (page - 1) * rows, rows);
		int total = this.service.getElementCountByName(tags);
		JSONObject result = new JSONObject();
		result.put("rows", elements);
		result.put("total", total);
		return result.toJSONString();
	}

	@RequestMapping(value = "/add", method = RequestMethod.POST)
	@ResponseBody
	public String addElement(Map<String, Object> model, @RequestParam(value = "tags", defaultValue = "") String tags,
			@RequestParam(value = "enName", defaultValue = "") String enName,
			@RequestParam(value = "id", defaultValue = "0") int id) {
		Tags element = new Tags();
		element.setEnName(enName);
		element.setTags(tags);
		element.setId(id);
		if (id == 0) {
			element.setCreateDate(new Date());
			this.service.addElement(element);
		} else {

			this.service.changeElement(element);
			List<App> appElements = this.appService.getListTagsId(id);
			AppCache cache = new AppCache();
			cache.deleteApps(appElements);
		}

		JSONObject result = new JSONObject();
		result.put("code", 0);
		result.put("msg", "success");
		return result.toJSONString();
	}

	@RequestMapping("/changestatus")
	@ResponseBody
	public String changeStatus(Map<String, Object> model,
			@RequestParam(value = "status", defaultValue = "0") int status,
			@RequestParam(value = "id", defaultValue = "0") int id) {

		this.service.changeStatus(status, id);
		List<App> appElements = this.appService.getListTagsId(id);
		AppCache cache = new AppCache();
		cache.deleteApps(appElements);
		JSONObject result = new JSONObject();
		result.put("code", 0);
		result.put("msg", "success");
		return result.toJSONString();
	}

	@RequestMapping(value = "/getelement")
	@ResponseBody
	public String getElement(Map<String, Object> model, @RequestParam(value = "id", defaultValue = "0") int id) {
		Tags element = this.service.getElementById(id);
		JSONObject result = new JSONObject();
		if (element != null) {
			result.put("code", 0);
		} else {
			result.put("code", 1);
		}
		result.put("msg", "success");
		result.put("data", element);
		return result.toJSONString();
	}

}
